<?php
class Default_Model_OrderTour extends Zend_Db_Table {
	protected $_name    = 'order_tour';
	protected $_primary = 'id';
	protected $_referenceMap = array(
			'Tour'=>array(
					'columns' => array('sub_menu_id'),
					'refTableClass' => 'Default_Model_Tour',
					'refColumns' => array('id'),
					'onDelete' => self::CASCADE,
			),
			'Customers'=>array(
					'columns' => array('tour_category_id'),
					'refTableClass' => 'Default_Model_Customers',
					'refColumns' => array('id'),
					'onDelete' => self::CASCADE,
			)
	
	);
	
	public function countItem($arrParam = null, $options = null) {
		$db = Zend_Registry::get('connectDb');
		$ssFilter = $arrParam['ssFilter'];
		$select = $db->select()
		             ->from('order_tour AS o', array('COUNT(o.id)  AS totalItem'));
	
		$result = $db->fetchOne($select);
		return $result;
	
	}
	
	// get list item
    public function listItem($arrParam = null, $options = null) {
		if($options['task'] == 'admin-list') {
			$db = Zend_Registry::get('connectDb');
			$paginator = $arrParam['paginator'];
			$ssFilter  = $arrParam['ssFilter'];
			//$db = Zend_Db::factory($adapter,$config);
	    	$select = $db->select()
		             ->from('order_tour AS o', array('id', 'quantity_person', 'status', 'time_anticipate'))
		             ->joinLeft('tours AS t', 'o.tours_id = t.id', array('name'))
		             ->joinLeft('customers AS c', 'o.customer_id = c.id', array('full_name'))
		             ->group('o.id');
	    	
	    	if($paginator['itemCountPerPage']>0) {
	    		$page     = $paginator['currentPage'];
	    		$rowCount = $paginator['itemCountPerPage'];
	    		$select->limitPage($page,$rowCount);
	    	}
	    	
	    	if(!empty($ssFilter['col']) && !empty($ssFilter['order'])) {
	    		$select->order($ssFilter['col'] . ' ' . $ssFilter['order']);
	    	}
	    	
	        
	       $result = $db->fetchAll($select);
		}
		
		return $result;
	} 
	
	// change status
	public function changeStatus($arrParam = null, $options = null) {
			
		// Multi change
		$cid = $arrParam['cid'];
		if(count($cid) > 0) {
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
				
			$ids   = implode(',', $cid);
			$where = 'id IN (' . $ids . ')';
			$data  =  array('status'=>$status);
			$this->update($data, $where);
		}
			
		// change only one item
		if($arrParam['id'] > 0) {
			$status = $arrParam['type'];
			$data   = array('status'=>$status);
			$where  = 'id = ' . $arrParam['id'];
			$this->update($data, $where);
		}
	}
	
	// get info item
	public function getItem($arrParam = null, $options = null) {
		if($options['task'] == 'admin-info') {
			$where  = 'id = ' . $arrParam['id'];
			$result = $this->fetchRow($where)->toArray();
	
			// customer
	        $customer = new Default_Model_Customers();
	        $customer = $customer->find($result['customer_id']);
	        $customer = $customer->toArray();
	        
	        $result['customer'] = $customer[0];
	        
	        // Tour
	        $tour = new Default_Model_Tour();
	        $tour = $tour->find($result['tours_id']);
	        $tour = $tour->toArray();
	         
	        $result['tour'] = $tour[0]['name'];
		}
		 
		return $result;
	}
	 
	// delete item
	public function deleteItem($arrParam = null, $options = null) {
		if($options['task'] == 'admin-delete') {
			$where = ' id = ' . $arrParam['id'];
			$this->delete($where);
		}
			
		if($options['task'] == 'admin-multi-delete') {
			$cid = $arrParam['cid'];
			if(count($cid) > 0) {
				$ids   = implode(',', $cid);
				$where = 'id IN (' . $ids . ')';
				$this->delete($where);
			}
		}
	}
	
}